package com.eptok.gw.szcups.service;

import ys8583.parse.IOMessage;

import com.eptok.gw.szcups.cups.CupsMessageControl;
import com.eptok.gw.szcups.cups.message.PackMessageUtil;
import com.eptok.gw.szcups.dao.JdbcDao;
import com.eptok.gw.szcups.dao.bo.Cupsreveral;
import com.eptok.gw.szcups.pojo.Relationship;
import com.eptok.gw.szcups.pojo.XmlMessage;
import com.eptok.gw.szcups.socket.client.factory.ClientFactory;
import com.eptok.gw.szcups.util.SerialNumber;

/**
 * 建立/撤销委托关系
 * @author guoq
 *
 */
public class RelationshipService extends BaseService {
	
	public RelationshipService(){
	}

	@Override
	public IOMessage process(XmlMessage xmlMessage) throws Exception {
		log.debug("RelationshipService process start");
		Relationship pay = (Relationship)xmlMessage;
		pay.setPayNo("ZD25"+pay.getPayType()+pay.getPayNo());
		IOMessage iomessagereq = PackMessageUtil.packRelationship(pay);
		long id = this.insertlog(pay, iomessagereq);
		
		ClientFactory.getInstance().sendMessage(CupsMessageControl.CHAN_CLIENT_KEY, iomessagereq);
		IOMessage iomessagersp = CupsMessageControl.getInstance().getChanMessageRsp((String)iomessagereq.getRspField(11));
		if(iomessagersp == null){
			//超时后需放到待冲正表等待冲正 
			pay.setStatus("Z6");
			pay.setRspSerialNumber(SerialNumber.seqno());
			JdbcDao jdbcDao = (JdbcDao)webApplicationContext.getBean("jdbcDao");
			Cupsreveral cupsreveral = new Cupsreveral();
			cupsreveral.setReqtraceId(pay.getReqSerialNumber());
			jdbcDao.insertCupsreveral(cupsreveral);
		}else{
			pay.setStatus((String)iomessagersp.getReqMesField(39));
			pay.setRspSerialNumber(SerialNumber.seqno());
		}
		this.updatelog(id, pay, iomessagersp);
		
		log.debug("RelationshipService process end");
		return iomessagersp;	
	}

	

}
